package org.tom.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tom.basic.util.JsonResult;
import org.tom.basic.util.PageList;
import org.tom.user.domain.Logininfo;
import org.tom.user.query.LogininfoQuery;
import org.tom.user.service.ILogininfoService;
import org.tom.user.query.LogininfoQuery;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class LogininfoController {
    @Autowired
    private ILogininfoService logininfoService;


    @ApiOperation(value = "用户查询单个用户",notes = "通过id进行查询")
    @GetMapping("{id}")  // get方法的请求，参数为id查询单条数据
    public Logininfo loadOne(@PathVariable("id") Long id){
        Logininfo logininfo = logininfoService.findById(id);
        return logininfo;
    }

    @ApiOperation(value = "查询所有的用户",notes = "不需要任何参数")
    @GetMapping // 查询所有
    public List<Logininfo> findAll(){
        return logininfoService.findAll();
    }

    @ApiOperation(value = "用户删除",notes = "通过id进行删除")
    @DeleteMapping("{id}") // 删除携带参数id
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            logininfoService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping // 修改和添加
    public JsonResult updateAndSave(@RequestBody Logininfo logininfo){
        try {
            if (logininfo.getId() == null){
                logininfoService.insert(logininfo);
            }else {
                logininfoService.update(logininfo);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @ApiOperation(value = "分页显示",notes = "传输分页需要的当前页和每页显示的条数")
    @PostMapping // 分页接受分页数据用对象
    public PageList<Logininfo> queryPage(@RequestBody LogininfoQuery logininfoQuery){
        return logininfoService.queryList(logininfoQuery);
    }

    @ApiOperation(value = "批量删除用户",notes = "传id集合")
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            logininfoService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }
}
